package com.hn.kiss.modules.sys.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hn.kiss.config.common.CommonPage;
import com.hn.kiss.config.common.CommonResult;
import com.hn.kiss.config.common.MyException;
import com.hn.kiss.config.query.QueryGenerator;
import com.hn.kiss.config.validdator.FlagValidator;
import com.hn.kiss.modules.sys.entity.DictDetail;
import com.hn.kiss.modules.sys.service.IDictDetailService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.util.List;


@Api(tags = "SysDictDetailController", description = "字典明细表管理")
@RestController
@RequestMapping("/sys/dict-detail")
@Validated
@Slf4j
public class DictDetailController {

    @Autowired
    IDictDetailService dictDetailService;

    @ApiOperation("字典明细表分页列表")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "pageNo", value = "当前页", required = true, dataType = "Integer", paramType = "query"),
        @ApiImplicitParam(name = "pageSize", value = "每页数量", required = true, dataType = "Integer", paramType = "query"),
    })
    @GetMapping("/pageInfo")
    public CommonPage<DictDetail> pageInfo(@NotNull Integer pageNo,
                                           @NotNull Integer pageSize,
                                           DictDetail dictDetail,
                                           HttpServletRequest request){
        Page<DictDetail> pageInfo = new Page<>(pageNo, pageSize);
        //构造查询条件
        QueryWrapper<DictDetail> qw = QueryGenerator.initQueryWrapper(dictDetail, request.getParameterMap());
        Page<DictDetail> page1 = dictDetailService.page(pageInfo, qw);
        return CommonPage.restPage(page1);
    }

    @ApiOperation("通过主键获取字典明细表")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "dictType", value = "字典类别代码", required = true, dataType = "String", paramType = "path"),
		@ApiImplicitParam(name = "dictCode", value = "字典代码", required = true, dataType = "String", paramType = "path")
    })
    @GetMapping("/{dictType}/{dictCode}")
    public CommonResult<DictDetail> getByPk(@NotBlank@PathVariable("dictType") String dictType,
                                            @NotBlank@PathVariable("dictCode") String dictCode){
        DictDetail dictDetail = dictDetailService.getByPk(dictType, dictCode);

        return CommonResult.success(dictDetail);
    }

    @ApiOperation("新增字典明细表")
    @PostMapping
    public CommonResult add(@Validated DictDetail dictDetail, BindingResult bindingResult) throws MyException {
        String dictCode = dictDetail.getDictCode().toUpperCase();
        check(dictDetail.getDictType(), dictCode);

        dictDetail.setDictCode(dictCode);
        boolean b = dictDetailService.save(dictDetail);
        if (b){
            return CommonResult.success("成功");
        }
        return CommonResult.failed("失败");
    }



    @ApiOperation("修改字典明细表")
    @PutMapping
    public CommonResult updateByPk(@Validated DictDetail dictDetail, BindingResult bindingResult){
        boolean b = dictDetailService.updateByPk(dictDetail);
        if (b){
            return CommonResult.success("成功");
        }
        return CommonResult.failed("失败");
    }

    @ApiOperation("修改字典状态")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "dictType", value = "字典类别", required = true, dataType = "String", paramType = "path"),
            @ApiImplicitParam(name = "dictCode", value = "字典代码", required = true, dataType = "String", paramType = "path"),
            @ApiImplicitParam(name = "state", value = "状态(1启用,0禁用)", required = true, dataType = "String", paramType = "query")
    })
    @PatchMapping("/state/{dictType}/{dictCode}")
    public CommonResult updateStateByPk(@NotBlank@PathVariable("dictType") String dictType,
                                        @NotBlank@PathVariable("dictCode") String dictCode,
                                        @NotBlank@FlagValidator(value = {"0", "1"}) String state){
        boolean b = dictDetailService.updateState(dictType, dictCode, state);
        if (b){
            return CommonResult.success("成功");
        }
        return CommonResult.failed("失败");
    }

    @ApiOperation("通过主键删除字典明细表")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "dictType", value = "字典类别代码", required = true, dataType = "String", paramType = "path"),
		@ApiImplicitParam(name = "dictCode", value = "字典代码", required = true, dataType = "String", paramType = "path")
    })
    @DeleteMapping("/{dictType}/{dictCode}")
    public CommonResult removeByPk(@NotBlank@PathVariable("dictType") String dictType,
						@NotBlank@PathVariable("dictCode") String dictCode){
        boolean b = dictDetailService.removeByPk(dictType, dictCode);
        if (b){
            return CommonResult.success("成功");
        }
        return CommonResult.failed("失败");
    }


    @ApiOperation("通过字典类别获取字典列表")
    @ApiImplicitParam(name = "dictType", value = "字典类别代码", required = true, dataType = "String", paramType = "path")
    @GetMapping("/{dictType}")
    public CommonResult<List<DictDetail>> getByDictType(@NotBlank@PathVariable("dictType") String dictType){
        List<DictDetail> dictDetails = dictDetailService.list(Wrappers.<DictDetail>lambdaQuery().eq(DictDetail::getDictType, dictType));
        return CommonResult.success(dictDetails);
    }

    /**
     * 校验字典
     * @param dictType
     * @param dictCode
     * @return
     */
    private void check(String dictType, String dictCode) throws MyException {
        DictDetail byPk = dictDetailService.getByPk(dictType, dictCode);
        if (byPk != null)
            throw new MyException("字典代码已存在!");
    }


}
